package Producer_Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/13
 * Time:18:39
 */
public class MyArrayBlockingList {
    private int head;
    private int tail;
    private int useSize;
    private final String[] array;
    //
    public MyArrayBlockingList(int capacity) {
        this.array = new String[capacity];
    }
    //
    public synchronized void put(String value) throws InterruptedException {
        while (isFull()) {
            this.wait();
        }
        array[tail] = value;
        tail++;
        if (tail >= array.length){
            tail = 0;
        }
        useSize++;
        this.notify();
    }
    //
    public synchronized String take() throws InterruptedException {
        while (useSize == 0) {
            this.wait();
        }
        String ret = array[head];
        head++;
        if (head >= array.length){
            head = 0;
        }
        useSize--;
        this.notify();
        return ret;
    }
    //
    public boolean isFull() {
        return useSize >= array.length;
    }
}
